package com.jackie.tongzhuang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;

public class InsertYinHangKaActivity extends AppCompatActivity implements View.OnClickListener {

    private DbClass dbClass = new DbClass(this);
    private EditText hmtext,mctext,edtext;
    private Spinner gsspinner;
    private Button savebutton;
    private TitleBar titleBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_yinhangka);
        initView();

        initData();

        titleBar.setOnClickedListener(new TitleBar.TitleBarClickListener() {
            @Override
            public void backClick(View v) {
                finish();
            }

            @Override
            public void submitClick(View v) {

            }
        });
    }

    private void initView(){
        hmtext = findViewById(R.id.yhk_add_zhhm);
        mctext = findViewById(R.id.yhk_add_zhmc);
        edtext = findViewById(R.id.yhk_add_ed);
        gsspinner = findViewById(R.id.yhk_add_zhgs);
        savebutton = findViewById(R.id.yhk_add_save);
        titleBar = findViewById(R.id.yhk_add_titlebar);
        savebutton.setOnClickListener(this);
    }

    private void initData(){
        ArrayAdapter<String> zhgsAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item,
                getResources().getStringArray(R.array.ZhangHuGuiShu));
        zhgsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        gsspinner.setAdapter(zhgsAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.yhk_add_save:
                if(mctext.getText().toString().isEmpty()){
                    Toast.makeText(this,"请输入账户名称",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(hmtext.getText().toString().isEmpty()){
                    Toast.makeText(this,"请输入账户号码",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(edtext.getText().toString().isEmpty()){
                    Toast.makeText(this,"请输入账户额度",Toast.LENGTH_SHORT).show();
                    return;
                }

                ContentValues values = new ContentValues();
                values.put("KaHao",hmtext.getText().toString());
                values.put("MingCheng",mctext.getText().toString());
                values.put("YinHang",gsspinner.getSelectedItem().toString());
                values.put("EDu",edtext.getText().toString());
                values.put("KeYongEDu",edtext.getText().toString());

                long id = dbClass.insert("YinHangKa",values);
                if(id >0){
                    Toast.makeText(this,"账户创建成功！",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(InsertYinHangKaActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }

                break;
        }
    }
}